#include <sys_head.h>

void countfile(long *charCount, int *wordCount, int *lineCount);

int main(int argc, char const *argv[])
{
    long charCount = 0;
    int wordCount = 0;
    int lineCount = 0;

    countfile(&charCount, &wordCount, &lineCount);

    printf("字符数: %ld\n", charCount);
    printf("单词数: %d\n", wordCount);
    printf("行数: %d\n", lineCount);

    return 0;
}

void countfile(long *charCount, int *wordCount, int *lineCount)
{
    FILE *fp = fopen("HQYJ.txt", "r");
    if (fp == NULL)
    {
        perror("无法打开文件");
        return;
    }
    int inword = 0;
    char ch;
    while ((ch = fgetc(fp))!= EOF)
    {
        (*charCount)++;
        if (ch == '\n')
        {
            (*lineCount)++;
        }
        if (ch == ' ' || ch == '\n' || ch == '\t')
        {
            inword = 0;
        }
        else if (!inword)
        {
            inword = 1;
            (*wordCount)++;
        }
    }
    if (ch!= '\n' && feof(fp))
    {
        (*lineCount)++;
    }

    fclose(fp);
}